package CCF_2014;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class T201403_3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String acceptOpsString = scanner.nextLine();
        int commandLineCount = Integer.valueOf(scanner.nextLine());
        List<Option> legalOps = new ArrayList<>();//按顺序存储一个命令行中合法的选项
        for(int i=0;i<commandLineCount;i++) { //依次读取命令行
            String commandLineOps[] = scanner.nextLine().split(" ");
            for (int j=1;j<commandLineOps.length;j++) {//判断一个命令行中所有选项的合法性
                int opType = getOpsType(commandLineOps[j], acceptOpsString);
                if (opType == ILLEGAL_OP  || (opType == LEGAL_ARG_OP &&j==commandLineOps.length-1))
                    break;//遇到非法选项直接终止当前命令行的分析
                char op = commandLineOps[j].charAt(1);
                String param = "";
                if (opType == LEGAL_ARG_OP) {
                    param = commandLineOps[++j];
                }
                Option option = new Option(op, param);
                insertOption(option, legalOps);//将合法的选项按规则插入到命令行选项集合中
            }
            System.out.print("Case "+(i+1)+": ");
            printCommandLine(legalOps);//打印命令行
            System.out.println();
        }
        scanner.close();
    }

    private static int ILLEGAL_OP = 0; //非法选项
    private static int LEGAL_OP = 1; //合法的无参数选项
    private static int LEGAL_ARG_OP = 2; //合法的带参数选项
    //判断命令行中选项类型的方法
    private static int getOpsType(String opString, String acceptOpsString) {
        if (opString.length() !=2 || opString.charAt(0) != '-' ) {
            return ILLEGAL_OP;
        }
        char op = opString.charAt(1);
        if (acceptOpsString.contains(op+":")) {
            return LEGAL_ARG_OP;
        } else if (acceptOpsString.contains(op + "")) {
            return LEGAL_OP;
        } else {
            return ILLEGAL_OP;
        }
    }

    //命令行选项类
    private static class Option {
        char op;//选项
        String param;//参数

        Option(char op, String param) {
            this.op = op;
            this.param = param;
        }
    }

    //按规则将选项插入到命令行选项集合中
    private static void insertOption(Option option, List<Option> optionList) {
        int i = 0;
        for (; i < optionList.size(); i++) {
            if (option.op==optionList.get(i).op) {
                optionList.get(i).param = option.param;
                break;
            } else if (option.op < optionList.get(i).op) {
                optionList.add(i,option);
                break;
            }
        }
        if (i==optionList.size()) {
            optionList.add(option);
        }
    }

    //按规则打印输出命令行
    private static void printCommandLine(List<Option> legalOps) {
        for (int k=0;k<legalOps.size();k++) {
            System.out.print("-" + legalOps.get(k).op + " ");
            if (legalOps.get(k).param!="") {
                System.out.print(legalOps.get(k).param + " ");
            }
        }
        legalOps.clear();
    }
}
